var count = 0;
var l=document.getElementById("name");
var l1=document.getElementById("scaleUnit");
function clickCreate(){
    var sure = true;
    var id=$("#name").val();
    var scoale=$("#scale").val();
    var icons = $("#scaleUnit").val();
    var lev = $("#lev").val();
    $("#namea").text("");
    $("#scalea").text("");
    $("#leva").text("");
    if(id==""){
        sure = false;
        $("#namea").text("编号不能为空！");
    }
    if(scoale==""){
        sure = false;
        $("#scalea").text("分类名称不能为空！");
    }
    if(lev==""){
         sure = false;
         $("#leva").text("分类级别不能为空！");
     }
    data1 = {
              "categoryIcon": icons,
              "categoryLevel": lev,
              "categoryName": scoale,
              "id": id
            }
    if(sure){
        $.ajax({
            type:'post',
            url:"/ebpa/category/",
            data:JSON.stringify(data1),
            contentType:"application/json",
            success: function (data) {
                var result = data;
                if(result==1){
                    window.location = "productclassify.html";
                }else{

                }
//                    $("#returnInfoShow").text("修改成功！");
//                    location.href="information.html";
            },
            error: function (XMLHttpRequest, textStatus, errorThrown){
            }
        });
    }
}
function clickUpdate(){
    var sure = true;
    var id=$("#name").val();
    var scoale=$("#scale").val();
    var icons = $("#scaleUnit").val();
    var lev = $("#lev").val();
    $("#namea").text("");
    $("#scalea").text("");
    $("#leva").text("");
    if(id==""){
        sure = false;
        $("#namea").text("编号不能为空！");
    }
    if(scoale==""){
        sure = false;
        $("#scalea").text("分类名称不能为空！");
    }
    if(lev==""){
         sure = false;
         $("#leva").text("分类级别不能为空！");
     }
    data1 = {
              "categoryIcon": icons,
              "categoryLevel": lev,
              "categoryName": scoale,
              "id": id
            }
    if(sure){
        $.ajax({
            type:'put',
            url:"/ebpa/category/",
            data:JSON.stringify(data1),
            contentType:"application/json",
            success: function (data) {
                var result = data;
                window.location = "productclassify.html";
//                    $("#returnInfoShow").text("修改成功！");
//                    location.href="information.html";
            },
            error: function (XMLHttpRequest, textStatus, errorThrown){
            }
        });
    }
}
function clickDelate(){
    var id=$("#name").val();
    $.ajax({
            type: 'delete',
            url: "/ebpa/category/?id="+id,
            dataType: "json",
            async: false,
            success: function (data) {
                var result = data;
                window.location = "productclassify.html";
            }
        });
}

$(document).ready(function() {
    "use strict";

    numSet();
    getProductClassifyInfo();
    showAreaList();
 });
 //点击输入框
 function clickNum(){
    $("#setNum123").removeAttr("class");
             var val=$("#name").val();
             if(val.length<1){
                 val = 0;
             }
             var val1 = 0;
             var val2 = 0;
             var val3 = 0;
             var val4 = 0;
             if(val.length>3){
                 val1 = val.substring(0,3);
                 if(val.length>6){
                     val2 = val.substring(3,6);
                     if(val.length>9){
                         val3 = val.substring(6,9);
                         if(val.length>12){
                              val4 = val.substring(9,12);
                          }else{
                              val4 = val.substring(9,val.length);;
                          }
                     }else{
                         val3 = val.substring(6,val.length);;
                     }
                 }else{
                     val2 = val.substring(3,val.length);
                 }
             }else{
                 val1 = val;
             }
             $("#s1").text(val1);
             $("#s2").text(val2);
             $("#s3").text(val3);
             $("#s4").text(val4);
 }
 //对输入的编号进行处理
 function numSet(){
     l.addEventListener("input", function () {
         $("#setNum123").removeAttr("class");
         var val=$(this).val();
         if(val.length<1){
             val = 0;
         }
         var val1 = 0;
         var val2 = 0;
         var val3 = 0;
         var val4 = 0;
         if(val.length>3){
             val1 = val.substring(0,3);
             if(val.length>6){
                 val2 = val.substring(3,6);
                 if(val.length>9){
                     val3 = val.substring(6,9);
                     if(val.length>12){
                          val4 = val.substring(9,12);
                      }else{
                          val4 = val.substring(9,val.length);;
                      }
                 }else{
                     val3 = val.substring(6,val.length);;
                 }
             }else{
                 val2 = val.substring(3,val.length);
             }
         }else{
             val1 = val;
         }
         $("#s1").text(val1);
         $("#s2").text(val2);
         $("#s3").text(val3);
         $("#s4").text(val4);
     });
     l1.addEventListener("input", function () {
        $("#setIcon").removeAttr("class");
        var ic = $("#scaleUnit").val();
        $("#setIcon").attr("class",ic);
     });
 }
 //点击按钮显隐分类
function showAreaList() {
    $("#zareaList").find("i").on("click",function (e) {

        if(count ==0){
            $(this).removeAttr("class");
            $(this).attr("class","fa fa-minus");
            $(this).parent("li").parent("div").children("div").removeAttr("class");
           count = 1;
        }else{
            $(this).removeAttr("class");
            $(this).attr("class","fa fa-plus");
            $(this).parent("li").parent("div").children("div").attr("class","tab-pane");
            count = 0;
        }
    });
    $("#zareaList").find("a").on("click",function (e) {
        var nameCode = $(this).attr("title");
        var nameLev = $(this).attr("data-areaid");
        var categoryIcon = $(this).attr("data-content-id");
        var named = $(this).text();
        $("#name").val(nameCode);
        $("#scale").val(named);
        $("#lev").val(nameLev);
        if(categoryIcon==null){
            $("#scaleUnit").val("");
        }else{
            $("#scaleUnit").val(categoryIcon);
        }
        numSet();
        $("#setIcon").removeAttr("class");
        var ic = $("#scaleUnit").val();
        $("#setIcon").attr("class",ic);
    });
}

var allClassifyInfo = [];
var allClassifyInfoSecond = [];
//获取产品分类
function getProductClassifyInfo(){
    $.ajax({
        type: 'get',
        url: "/ebpa/category/categoryByList?id=120000000000&level=1",
        dataType: "json",
        async: false,
        success: function (data) {
            var result = data;
            allClassifyInfo = result;
            for(var i=0;i<allClassifyInfo.length;i++){
                var temp = allClassifyInfo[i].nextCategory;
                for(var j=0;j<temp.length;j++){
                    getSecondInfo(temp[j].id);
                    allClassifyInfo[i].nextCategory[j].nextCategory = allClassifyInfoSecond;
                }
            }
            var vue = new Vue({
                el:'#zareaList',
                data: {
                        message:allClassifyInfo
                    }
            });
        }
    });
}
function getSecondInfo(id){
    $.ajax({
            type: 'get',
            url: "/ebpa/category/categoryByList?id="+id+"&level=3",
            dataType: "json",
            async: false,
            success: function (data) {
                var result = data;
                allClassifyInfoSecond = result;

            }
        });
}

var productClassifyInfo = [{
    classifyId: "100100100100",
    classifyName: "蔬菜种植",
    ico: "王贵银1",
    childrenOne: [{
        classifyId: "100101100100",
        classifyName: "蔬菜作物",
        ico: "s1",
        childrenOne: [{
             classifyId: "100101100100",
             classifyName: "水果作物",
             ico: "s1",
             childrenOne: []
             },{
            classifyId: "100101100200",
            classifyName: "水果作物",
            ico: "s1",
            childrenOne: []
             }]
    },{
          classifyId: "100102100100",
          classifyName: "水果作物",
          ico: "s1",
          childrenOne: [{
                 classifyId: "100102100100",
                 classifyName: "水果作物",
                 ico: "s1",
                 childrenOne: []
                 },{
                classifyId: "100102100200",
                classifyName: "水果作物",
                ico: "s1",
                childrenOne: []
                 }]
      },{
            classifyId: "100103100100",
            classifyName: "粮油作物",
            ico: "s1",
            childrenOne: [{
                   classifyId: "100103100100",
                   classifyName: "水果作物",
                   ico: "s1",
                   childrenOne: []
                   },{
                  classifyId: "100103100200",
                  classifyName: "水果作物",
                  ico: "s1",
                  childrenOne: []
                   }]
        },
        {
            classifyId: "100104100100",
            classifyName: "经济作物",
            ico: "s1",
            childrenOne: []
        }]
},{
    classifyId: "100200100100",
    classifyName: "养殖水产",
    ico: "王贵银1",
    childrenOne: [{
        classifyId: "100201100100",
        classifyName: "水产养殖",
        ico: "s1",
        childrenOne: []
    },{
          classifyId: "100202100100",
          classifyName: "牲畜养殖",
          ico: "s1",
          childrenOne: []
      },{
            classifyId: "100203100100",
            classifyName: "家禽养殖",
            ico: "s1",
            childrenOne: []
        },
        {
            classifyId: "100204100100",
            classifyName: "其他养殖",
            ico: "s1",
            childrenOne: []
        }]
}]

